#include<iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <set>
#include <map>
using namespace std;
class Solution {
public:
    int n, m;
    vector<vector<int>> generateMatrix(int nn) {
        n = m = nn;
        vector<vector<int>>ans;
        ans.resize(n);
        for (int i = 0; i < n; i++)
        {
            ans[i].resize(n);
            for (int j = 0; j < n; j++)
            {
                ans[i][j] = getIndex(i, j) + 1;
            }
        }
        return ans;
    }
    int getC(int i, int j)
    {

        j = j < m - j - 1 ? j : m - j - 1;
        i = i < n - i - 1 ? i : n - i - 1;
        return i < j ? i : j;
    }
    int getIndex(int i, int j)
    {
        int t = getC(i, j);
        int nt = n - 2 * t;
        int mt = m - 2 * t;
        int index = n * m - nt * mt;;
        if (t == i) index += j - t;
        else if (t == m - j - 1)index += mt + i - t - 1;
        else if (t == n - i - 1) index += nt + mt + m - t - j - 3;
        else if (t == j)index += mt * 2 + nt + n - t - i - 4;
        return index;
    }
};